package com.shuoyue.ycgk.ui.recruitment.resume;

import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpActivity;
import com.shuoyue.ycgk.entity.Resume;
import com.shuoyue.ycgk.entity.address.AddressBean;
import com.shuoyue.ycgk.ui.recruitment.resume.certificate.CertificateActivity;
import com.shuoyue.ycgk.ui.recruitment.resume.major.SelectMajorActivityTab;
import com.shuoyue.ycgk.utils.XToast;
import com.shuoyue.ycgk.views.dialog.CustomPicker;
import com.shuoyue.ycgk.views.dialog.addresspicker.AddressPickerDialog;

import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.addapp.pickers.picker.DatePicker;

public class MyResumeActivitybackup extends BaseMvpActivity<MyResumeContract.Presenter>
        implements MyResumeContract.View {

    @BindView(R.id.page_title)
    TextView pageTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.currentAcc)
    TextView currentAcc;
    @BindView(R.id.birthday)
    TextView birthday;
    @BindView(R.id.sex)
    TextView sex;
    @BindView(R.id.household)
    TextView household;
    @BindView(R.id.political)
    TextView political;
    @BindView(R.id.education)
    TextView education;
    @BindView(R.id.nature)
    TextView nature;
    @BindView(R.id.major)
    TextView major;
    @BindView(R.id.graduationTime)
    TextView graduationTime;
    @BindView(R.id.degree)
    TextView degree;
    @BindView(R.id.certificate)
    TextView certificate;
    @BindView(R.id.workExperience)
    TextView workExperience;
    @BindView(R.id.jobName)
    TextView jobName;
    @BindView(R.id.serviceExperience)
    TextView serviceExperience;

    Resume resume;

    /**
     * 初始化layouy
     */
    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_resume;
    }

    /**
     * 初始化布局
     */
    @Override
    protected void initView() {
        pageTitle.setText("我的简历");
        tvRight.setVisibility(View.VISIBLE);
        tvRight.setText("保存");
        tvRight.setTextColor(Color.parseColor("#ffffff"));
    }

    @Override
    public void initData() {
        super.initData();
        mPresenter = new MyResumeContract.Presenter();
        mPresenter.attachView(this);
        mPresenter.getResume();
    }

    @Override
    public void getResumeSuc(Resume resume) {
        if (resume == null) {
            resume = new Resume();
        }
        this.resume = resume;
        birthday.setText(resume.getBirthday() == null ? "" : resume.getBirthday());
        sex.setText(resume.getSex() == null ? "" : resume.getSex());
        household.setText(resume.getHousehold() == null ? "" : resume.getHousehold());
        political.setText(resume.getPolitical() == null ? "" : resume.getPolitical());
        education.setText(resume.getEducation() == null ? "" : resume.getEducation());
        nature.setText(resume.getNature() == null ? "" : resume.getNature());
        major.setText(resume.getMajor() == null ? "" : resume.getMajor());
        graduationTime.setText(resume.getGraduationTime() == null ? "" : resume.getGraduationTime());
        degree.setText(resume.getDegree() == null ? "" : resume.getDegree());
        certificate.setText(resume.getCertificate() == null ? "" : resume.getCertificate());
        workExperience.setText(resume.getWorkExperience() == null ? "" : resume.getWorkExperience());
        jobName.setText(resume.getJobName() == null ? "" : resume.getJobName());
        serviceExperience.setText(resume.getServiceExperience() == null ? "" : resume.getServiceExperience());
        currentAcc.setText(resume.getDonePercent() == null ? "0" : "" + resume.getDonePercent());

    }

    @Override
    public void saveSuc() {
        mPresenter.getResume();
        XToast.toast(mContext, "保存成功");
    }


    @OnClick({R.id.back, R.id.tv_right,
            R.id.birthday, R.id.sex,
            R.id.household, R.id.political,
            R.id.education, R.id.nature,
            R.id.major, R.id.graduationTime,
            R.id.degree, R.id.certificate,
            R.id.workExperience, R.id.jobName,
            R.id.serviceExperience})
    public void onViewClicked(View view) {
        if (resume == null) {
            return;
        }
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.tv_right:
                save();
                break;
            case R.id.birthday:
                showBirthDayPicker();
                break;
            case R.id.sex:
                showSexPicker();
                break;
            case R.id.household:
                mPresenter.getArea(0, 1, null);
                break;
            case R.id.political:
                showpolitical();
                break;
            case R.id.education:
                showeducation();
                break;
            case R.id.nature:
                shownature();
                break;
            case R.id.major:
                startActivityForResult(new Intent(mContext, SelectMajorActivityTab
                        .class), 34);
//                showmajor();
                break;
            case R.id.graduationTime:
                showgraduationTime();
                break;
            case R.id.degree:
                showdegree();
                break;
            case R.id.certificate:
                startActivityForResult(new Intent(mContext, CertificateActivity
                        .class), 35);
//                showcertificate();
                break;
            case R.id.workExperience:
                showworkExperience();
                break;
            case R.id.jobName:
                showjobName();
                break;
            case R.id.serviceExperience:
                showserviceExperience();
                break;
        }
    }

    void save() {
        if (TextUtils.isEmpty(resume.getBirthday())) {
            toast("请选择出生日期");
            return;
        }
        if (TextUtils.isEmpty(resume.getEducation())) {
            toast("请选择最高学历");
            return;
        }
        if (TextUtils.isEmpty(resume.getMajor())) {
            toast("请选择专业");
            return;
        }

        mPresenter.saveResume(resume);
    }

    void showSexPicker() {
        String[] dataArray = new String[]{"男", "女"};
        CustomPicker picker = new CustomPicker(this, dataArray, "请选择性别");
        picker.setSelectedTextColor(ContextCompat.getColor(mContext, R.color.all_orange_color));
        picker.setUnSelectedTextColor(ContextCompat.getColor(mContext, R.color.text_main_title));
        picker.setCanceledOnTouchOutside(true);
        picker.setOffset(1);//显示的条目的偏移量，条数为（offset*2+1）
//        picker.setGravity(Gravity.CENTER);//居中
        picker.setOnItemPickListener((position, option) -> {
            picker.dismiss();
            resume.setSex(option);
            sex.setText(option);
        });
        picker.show();
    }

    void showhousehold(AddressPickerDialog addressPickerDialog) {

        addressPickerDialog.setPickerFinishCallback(beans -> {
            if (beans.length == 3 && beans[0] != null
                    && beans[1] != null && beans[2] != null) {
                String location = beans[0].getName() +
                        beans[1] +
                        beans[2];
                resume.setHousehold(location);
                household.setText(location);
                addressPickerDialog.dismiss();
            } else {
                XToast.toast(mContext, "请完成选择");
            }

        });
        addressPickerDialog.setPickerLevel1(beans -> mPresenter.getArea(beans.getId(), 2, addressPickerDialog));
        addressPickerDialog.setPickerLevel2((beans1, beans2) -> mPresenter.getArea(beans2.getId(), 3, addressPickerDialog));
        if (!addressPickerDialog.isShowing()) {
            addressPickerDialog.show();
        }
    }

    @Override
    public void setAreas(List<AddressBean> areas, int pid, int desLevel, AddressPickerDialog addressPickerDialog) {
        if (addressPickerDialog == null) {
            addressPickerDialog = new AddressPickerDialog(mContext, 3);
        }
        if (desLevel == 1) {
            addressPickerDialog.setLevl1Data(areas);
            addressPickerDialog.setLevl2Data(null);
            addressPickerDialog.setLevl3Data(null);
            addressPickerDialog.show();
        }

        if (desLevel == 2) {
            addressPickerDialog.setLevl2Data(areas);
            addressPickerDialog.setLevl3Data(null);
        }

        if (desLevel == 3) {
            addressPickerDialog.setLevl3Data(areas);
        }
        showhousehold(addressPickerDialog);
    }

    /**
     * 政治面貌
     */
    void showpolitical() {
        String[] dataArray = new String[]{"中国共产党党员(含预备党员)", "共青团员", "民主党派", "群众"};
        CustomPicker picker = new CustomPicker(this, dataArray, "政治面貌");
        picker.setSelectedTextColor(ContextCompat.getColor(mContext, R.color.all_orange_color));
        picker.setUnSelectedTextColor(ContextCompat.getColor(mContext, R.color.text_main_title));
        picker.setCanceledOnTouchOutside(true);
        picker.setOffset(1);//显示的条目的偏移量，条数为（offset*2+1）
//        picker.setGravity(Gravity.CENTER);//居中
        picker.setOnItemPickListener((position, option) -> {
            picker.dismiss();
            resume.setPolitical(option);
            political.setText(option);
        });
        picker.show();
    }

    /**
     * 学历
     */
    void showeducation() {
        String[] dataArray = new String[]{"高中", "中专", "专科", "本科", "硕士研究生", "博士研究生"};
        CustomPicker picker = new CustomPicker(this, dataArray, "最高学历");
        picker.setSelectedTextColor(ContextCompat.getColor(mContext, R.color.all_orange_color));
        picker.setUnSelectedTextColor(ContextCompat.getColor(mContext, R.color.text_main_title));
        picker.setCanceledOnTouchOutside(true);
        picker.setOffset(1);//显示的条目的偏移量，条数为（offset*2+1）
//        picker.setGravity(Gravity.CENTER);//居中
        picker.setOnItemPickListener((position, option) -> {
            picker.dismiss();
            resume.setEducation(option);
            education.setText(option);
        });
        picker.show();
    }

    /**
     * 学历性质
     */
    void shownature() {
        String[] dataArray = new String[]{"全日制", "非全日制"};
        CustomPicker picker = new CustomPicker(this, dataArray, "学历性质");
        picker.setSelectedTextColor(ContextCompat.getColor(mContext, R.color.all_orange_color));
        picker.setUnSelectedTextColor(ContextCompat.getColor(mContext, R.color.text_main_title));
        picker.setCanceledOnTouchOutside(true);
        picker.setOffset(1);//显示的条目的偏移量，条数为（offset*2+1）
//        picker.setGravity(Gravity.CENTER);//居中
        picker.setOnItemPickListener((position, option) -> {
            picker.dismiss();
            resume.setNature(option);
            nature.setText(option);
        });
        picker.show();
    }

    /**
     * 专业
     */
    void showmajor() {
        String[] dataArray = new String[]{"工科类", "医学类", "理科类", "文科类"};
        CustomPicker picker = new CustomPicker(this, dataArray, "专业");
        picker.setSelectedTextColor(ContextCompat.getColor(mContext, R.color.all_orange_color));
        picker.setUnSelectedTextColor(ContextCompat.getColor(mContext, R.color.text_main_title));
        picker.setCanceledOnTouchOutside(true);
        picker.setOffset(1);//显示的条目的偏移量，条数为（offset*2+1）
//        picker.setGravity(Gravity.CENTER);//居中
        picker.setOnItemPickListener((position, option) -> {
            picker.dismiss();
            resume.setMajor(option);
            major.setText(option);
        });
        picker.show();
    }

    /**
     * 毕业时间
     */
    void showgraduationTime() {
        DatePicker datePicker = new DatePicker(this);
        datePicker.setCanceledOnTouchOutside(true);
        datePicker.setSelectedTextColor(ContextCompat.getColor(mContext, R.color.all_orange_color));
        datePicker.setUnSelectedTextColor(ContextCompat.getColor(mContext, R.color.text_main_title));
        Calendar cale = Calendar.getInstance();
        int year = cale.get(Calendar.YEAR);
        int month = cale.get(Calendar.MONTH) + 1;
        int day = cale.get(Calendar.DATE);
        datePicker.setRangeStart(1980, 1, 1);
        datePicker.setRangeEnd(year, month, day);
        datePicker.setOnDatePickListener((DatePicker.OnYearMonthDayPickListener) (s, s1, s2) -> {
            resume.setGraduationTime(s + "-" + s1 + "-" + s2);
            graduationTime.setText(resume.getGraduationTime());
        });
        datePicker.show();
    }

    /**
     * 学位
     */
    void showdegree() {
        String[] dataArray = new String[]{"无", "学士", "硕士", "博士"};
        CustomPicker picker = new CustomPicker(this, dataArray, "学位");
        picker.setSelectedTextColor(ContextCompat.getColor(mContext, R.color.all_orange_color));
        picker.setUnSelectedTextColor(ContextCompat.getColor(mContext, R.color.text_main_title));
        picker.setCanceledOnTouchOutside(true);
        picker.setOffset(1);//显示的条目的偏移量，条数为（offset*2+1）
//        picker.setGravity(Gravity.CENTER);//居中
        picker.setOnItemPickListener((position, option) -> {
            picker.dismiss();
            resume.setDegree(option);
            degree.setText(option);
        });
        picker.show();
    }

    /**
     * 资格证书
     */
    void showcertificate() {
        String[] dataArray = new String[]{"暂无选项"};
        CustomPicker picker = new CustomPicker(this, dataArray, "资格证书");
        picker.setSelectedTextColor(ContextCompat.getColor(mContext, R.color.all_orange_color));
        picker.setUnSelectedTextColor(ContextCompat.getColor(mContext, R.color.text_main_title));
        picker.setCanceledOnTouchOutside(true);
        picker.setOffset(1);//显示的条目的偏移量，条数为（offset*2+1）
//        picker.setGravity(Gravity.CENTER);//居中
        picker.setOnItemPickListener((position, option) -> {
            picker.dismiss();
            resume.setCertificate(option);
            certificate.setText(option);
        });
        picker.show();
    }

    /**
     * 工作经验
     */
    void showworkExperience() {
        String[] dataArray = new String[]{"无工作经验", "一年", "两年", "三年", "四年", "5年及以上"};
        CustomPicker picker = new CustomPicker(this, dataArray, "工作经验");
        picker.setSelectedTextColor(ContextCompat.getColor(mContext, R.color.all_orange_color));
        picker.setUnSelectedTextColor(ContextCompat.getColor(mContext, R.color.text_main_title));
        picker.setCanceledOnTouchOutside(true);
        picker.setOffset(1);//显示的条目的偏移量，条数为（offset*2+1）
//        picker.setGravity(Gravity.CENTER);//居中
        picker.setOnItemPickListener((position, option) -> {
            picker.dismiss();
            resume.setWorkExperience(option);
            workExperience.setText(option);
        });
        picker.show();
    }

    /**
     * 目前职位
     */
    void showjobName() {
        String[] dataArray = new String[]{"国家公务人员", "事业单位工作人员", "国有企业工作人员", "三资、民营等企业工作人员", "自由职业者", "应届毕业生", "留学回国人员", "待业人员", "其他人员"};
        CustomPicker picker = new CustomPicker(this, dataArray, "目前职位");
        picker.setSelectedTextColor(ContextCompat.getColor(mContext, R.color.all_orange_color));
        picker.setUnSelectedTextColor(ContextCompat.getColor(mContext, R.color.text_main_title));
        picker.setCanceledOnTouchOutside(true);
        picker.setOffset(1);//显示的条目的偏移量，条数为（offset*2+1）
//        picker.setGravity(Gravity.CENTER);//居中
        picker.setOnItemPickListener((position, option) -> {
            picker.dismiss();
            resume.setJobName(option);
            jobName.setText(option);
        });
        picker.show();
    }

    /**
     * 基层服务经验
     */
    void showserviceExperience() {
        String[] dataArray = new String[]{"无服务基层工作经历", "四项目人员", "村官", "西部志愿者", "退役士兵", "特岗教育教师", "三支一扶"};
        CustomPicker picker = new CustomPicker(this, dataArray, "基层服务经验");
        picker.setSelectedTextColor(ContextCompat.getColor(mContext, R.color.all_orange_color));
        picker.setUnSelectedTextColor(ContextCompat.getColor(mContext, R.color.text_main_title));
        picker.setCanceledOnTouchOutside(true);
        picker.setOffset(1);//显示的条目的偏移量，条数为（offset*2+1）
//        picker.setGravity(Gravity.CENTER);//居中
        picker.setOnItemPickListener((position, option) -> {
            picker.dismiss();
            resume.setServiceExperience(option);
            serviceExperience.setText(option);
        });
        picker.show();
    }

    void showBirthDayPicker() {
        DatePicker datePicker = new DatePicker(this);
        datePicker.setCanceledOnTouchOutside(true);
        datePicker.setSelectedTextColor(ContextCompat.getColor(mContext, R.color.all_orange_color));
        datePicker.setUnSelectedTextColor(ContextCompat.getColor(mContext, R.color.text_main_title));
        datePicker.setCanLinkage(true);
        datePicker.setOnlyCenterLabel(true);
        Calendar cale = Calendar.getInstance();
        int year = cale.get(Calendar.YEAR);
        int month = cale.get(Calendar.MONTH) + 1;
        int day = cale.get(Calendar.DATE);
        datePicker.setRangeStart(1980, 1, 1);
        datePicker.setRangeEnd(year, month, day);
        datePicker.setOnDatePickListener((DatePicker.OnYearMonthDayPickListener) (s, s1, s2) -> {
            resume.setBirthday(s + "-" + s1 + "-" + s2);
            birthday.setText(resume.getBirthday());
        });
        datePicker.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 34 && resultCode == RESULT_OK) {
            String majorresult = data.getStringExtra("name");
            major.setText(majorresult);
            resume.setMajor(majorresult);
        } else if (requestCode == 35 && resultCode == RESULT_OK) {
//            mPresenter.getResume();
            String certName = data.getStringExtra("name");
            certificate.setText(certName);
        }
    }
}
